#!/system/bin/sh
# EAS parameter adjustment for SDM 845
# https://github.com/yc9559/cpufreq-interactive-opt
# Author: yc9559
# Generated at: Sat May  5 2018

# $1:value $2:file path
function set_value() {
	if [ -f $2 ]; then
		chown 0.0 $2
		chmod 0666 $2
		echo $1 > $2
		chmod 0444 $2
	fi
}

# $1:cpu0 $2:timer_rate $3:value
function set_param() {
	echo $3 > /sys/devices/system/cpu/$1/cpufreq/schedutil/$2
}

action=$1
setenforce 0

# RunOnce
if [ ! -f /dev/project_wipe_runonce ]; then
	# set flag
	touch /dev/project_wipe_runonce

	# Perfd, nothing to worry about, if error the script will continue
	stop perfd

	set_value 90 /proc/sys/kernel/sched_spill_load
	set_value 0 /proc/sys/kernel/sched_boost
	set_value 1 /proc/sys/kernel/sched_prefer_sync_wakee_to_waker
	set_value 85 /proc/sys/kernel/sched_downmigrate
	set_value 95 /proc/sys/kernel/sched_upmigrate
	set_value 2000000 /proc/sys/kernel/sched_freq_inc_notify
	set_value 1000000 /proc/sys/kernel/sched_freq_dec_notify

	set_value 50 /sys/module/cpu_boost/parameters/input_boost_ms
	set_value 0 /sys/module/msm_performance/parameters/touchboost
fi

set_value 1 /sys/devices/system/cpu/cpu4/online
chown 0.0 /sys/devices/system/cpu/cpu0/cpufreq/schedutil/*
chown 0.0 /sys/devices/system/cpu/cpu4/cpufreq/schedutil/*
chmod 0666 /sys/devices/system/cpu/cpu0/cpufreq/schedutil/*
chmod 0666 /sys/devices/system/cpu/cpu4/cpufreq/schedutil/*

if [ "$action" = "powersave" ]; then
	set_value "4:1980000" /sys/module/msm_performance/parameters/cpu_max_freq
	set_value "0:1080000 4:0" /sys/module/cpu_boost/parameters/input_boost_freq
	set_value 1 /sys/devices/system/cpu/cpu4/core_ctl/min_cpus
	set_value 2 /sys/devices/system/cpu/cpu4/core_ctl/max_cpus
	set_param cpu0 hispeed_freq 1080000
	set_param cpu0 hispeed_load 97
	set_param cpu0 pl 0
	set_param cpu4 hispeed_freq 980000
	set_param cpu4 hispeed_load 97
	set_param cpu4 pl 0
fi

if [ "$action" = "balance" ]; then
	set_value "4:2480000" /sys/module/msm_performance/parameters/cpu_max_freq
	set_value "0:1080000 4:0" /sys/module/cpu_boost/parameters/input_boost_freq
	set_value 2 /sys/devices/system/cpu/cpu4/core_ctl/min_cpus
	set_value 4 /sys/devices/system/cpu/cpu4/core_ctl/max_cpus
	set_param cpu0 hispeed_freq 1180000
	set_param cpu0 hispeed_load 95
	set_param cpu0 pl 1
	set_param cpu4 hispeed_freq 1180000
	set_param cpu4 hispeed_load 97
	set_param cpu4 pl 1
fi

if [ "$action" = "performance" ]; then
	set_value "4:2880000" /sys/module/msm_performance/parameters/cpu_max_freq
	set_value "0:1180000 4:0" /sys/module/cpu_boost/parameters/input_boost_freq
	set_value 2 /sys/devices/system/cpu/cpu4/core_ctl/min_cpus
	set_value 4 /sys/devices/system/cpu/cpu4/core_ctl/max_cpus
	set_param cpu0 hispeed_freq 1180000
	set_param cpu0 hispeed_load 90
	set_param cpu0 pl 1
	set_param cpu4 hispeed_freq 1480000
	set_param cpu4 hispeed_load 95
	set_param cpu4 pl 1
fi

if [ "$action" = "fast" ]; then
	set_value "4:2880000" /sys/module/msm_performance/parameters/cpu_max_freq
	set_value "0:1480000 4:1680000" /sys/module/cpu_boost/parameters/input_boost_freq
	set_value 4 /sys/devices/system/cpu/cpu4/core_ctl/min_cpus
	set_value 4 /sys/devices/system/cpu/cpu4/core_ctl/max_cpus
	set_param cpu0 hispeed_freq 1480000
	set_param cpu0 hispeed_load 85
	set_param cpu0 pl 1
	set_param cpu4 hispeed_freq 1680000
	set_param cpu4 hispeed_load 85
	set_param cpu4 pl 1
fi

chmod 0444 /sys/devices/system/cpu/cpu0/cpufreq/schedutil/*
chmod 0444 /sys/devices/system/cpu/cpu4/cpufreq/schedutil/*

exit 0